#! /bin/bash
# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4:

# Regression tests for toktx

# -------------------------------------------------------------------------
#
# Copyright 2010-2020 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0
#
# -------------------------------------------------------------------------

# Depth of this script relative to the project root
depth=..

# Change dir to the testimages folder, a child of the script location...
cd $(dirname $0)/testimages
# ...and adjust depth
depth=$depth/..

# Make paths relative to the testimages directory.
ktx_root=$depth
toktx_vs2013=$ktx_root/build/msvs/win/vs2013/x64/Release/toktx.exe
toktx_vs2015=$ktx_root/build/msvs/win/vs2015/x64/Release/toktx.exe
toktx_cmake=$ktx_root/build/cmake/linux/Release/toktx
toktx_cmake_d=$ktx_root/build/cmake/linux/Debug/toktx
toktx_make=$ktx_root/build/make/linux/out/Release/toktx
toktx_make_d=$ktx_root/build/make/linux/out/Debug/toktx

declare -i numtests=0
declare -i passed=0
declare -i failed=0

# Ensure test is not polluted by user environment
unset TOKTX_OPTIONS

if [ -n "$1" -a -x "$1" ]; then
  toktx="$1"
elif [ -x "$toktx_vs2013" ]; then
  toktx=$toktx_vs2013
elif [ -x "$toktx_vs2015" ]; then
  toktx=$toktx_vs2015
elif [ -x "$toktx_cmake" ]; then
  toktx=$toktx_cmake
elif [ -x "$toktx_cmake_d" ]; then
  toktx=$toktx_cmake_d
elif [ -x "$toktx_make" ]; then
  toktx=$toktx_gmake
elif [ -x "$toktx_make_d" ]; then
  toktx=$toktx_gmake
else
  echo $0: None of $toktx_vs2013, $toktx_vs2015, $toktx_gmake or $toktx_cmake found.
  echo $0: Aborting test
  exit 1
fi

numtests=$numtests+1
if $toktx --help 2> /dev/null; then
  passed=$passed+1
else
  echo "--help not recognized"
  failed=$failed+1
fi

numtests=$numtests+1
if $toktx --version 2> /dev/null; then
  passed=$passed+1
else
  echo "--version not recognized"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --foobar 2> /dev/null; then
  passed=$passed+1
else
  echo "invalid option --foobar accepted"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --automipmap --mipmaps a b 2> /dev/null; then
  passed=$passed+1
else
  echo "Simultaneous --automipmap & --mipmaps allowed"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --alpha a b 2> /dev/null; then
  passed=$passed+1
else
  echo "--alpha allowed"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --luminance a b 2> /dev/null; then
  passed=$passed+1
else
  echo "--luminance allowed"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --zcmp --etc1s a b 2> /dev/null; then
  passed=$passed+1
else
  echo "Simultaneous --etc1s & --zcmp allowed"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --etc1s --uastc a b 2> /dev/null; then
  passed=$passed+1
else
  echo "Simultaneous --etc1s & --uastc allowed"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --scale 0.5 --resize 10x40 a b 2> /dev/null; then
  passed=$passed+1
else
  echo "Simultaneous --scale & --resize allowed"
  failed=$failed+1
fi

numtests=$numtests+1
if ! $toktx --mipmap --resize 10x40 a b c 2> /dev/null; then
  passed=$passed+1
else
  echo "Simultaneous --mipmap & --resize allowed"
  failed=$failed+1
fi


function cmpktx () {
  if diff $1 $2; then
    passed=$passed+1
    rm $1
    return 0
  else
	failed=$failed+1
    echo "Created ktx file differs from target $2. Bad file saved in $PWD/$1"
    return 1
  fi
}

function mktmp() {
  # Git Bash does not include mktemp.
  if which mktemp > /dev/null; then
    mktemp toktx.XXXX
  else
    toktx.$numtests
  fi
}

# Generate ktx file and compare with reference
# gencmpktx <reference> <toktx args> <toktx infile> ...
function gencmpktx() {
  numtests=$numtests+1
  local args
  local reference=$1; shift
  local tempfile=$(mktmp)
  for i in $*; do
	if [ ${i:0:2} == "--" ]; then
	  args="$args $i"
    shift
	fi
  done
  #echo $toktx $args $tempfile $*
  if $toktx $args $tempfile $*; then
    cmpktx $tempfile $reference
  fi
}

#---------------- Tests start here -----------------------

gencmpktx rgb-reference.ktx --lower_left_maps_to_s0t0 --nometadata ../srcimages/rgb.ppm
gencmpktx rgb-amg-reference.ktx --automipmap --lower_left_maps_to_s0t0 --linear --nometadata ../srcimages/rgb.ppm
TOKTX_OPTIONS="--lower_left_maps_to_s0t0 --nometadata" gencmpktx orient-up.ktx ../srcimages/up.ppm
gencmpktx orient-up-metadata.ktx --lower_left_maps_to_s0t0 ../srcimages/up.ppm
gencmpktx orient-down-metadata.ktx ../srcimages/up.ppm
gencmpktx rgba-reference.ktx --lower_left_maps_to_s0t0 --nometadata ../srcimages/rgba.pam
gencmpktx rgb-mipmap-reference.ktx --lower_left_maps_to_s0t0 --mipmap --nometadata ../srcimages/level0.ppm ../srcimages/level1.ppm ../srcimages/level2.ppm ../srcimages/level3.ppm ../srcimages/level4.ppm ../srcimages/level5.ppm ../srcimages/level6.ppm
gencmpktx rgb-mipmap-reference-u.ktx2 --test --t2 --mipmap ../srcimages/level0.ppm ../srcimages/level1.ppm ../srcimages/level2.ppm ../srcimages/level3.ppm ../srcimages/level4.ppm ../srcimages/level5.ppm ../srcimages/level6.ppm
if [ $(uname) == "Darwin" ]; then
  # Run only on macOS until we figure out the Basis compressor non-determinancy.
  gencmpktx alpha_simple_basis.ktx2 --test --bcmp ../srcimages/alpha_simple.png
  gencmpktx kodim17_basis.ktx2 --test --bcmp ../srcimages/kodim17.png
  gencmpktx color_grid_basis.ktx2 --test --bcmp ../srcimages/color_grid.png
  gencmpktx cimg5293_uastc.ktx2 --uastc --genmipmap --test ../srcimages/CIMG5293.jpg
  gencmpktx cimg5293_uastc_zstd.ktx2 --zcmp --uastc --genmipmap --test ../srcimages/CIMG5293.jpg
fi

filelist=$(mktemp)
printf "../srcimages/level0.ppm\n../srcimages/level1.ppm\n../srcimages/level2.ppm\n../srcimages/level3.ppm\n../srcimages/level4.ppm\n../srcimages/level5.ppm\n../srcimages/level6.ppm" > $filelist
gencmpktx rgb-mipmap-reference.ktx --lower_left_maps_to_s0t0 --mipmap --nometadata @$filelist
rm $filelist

numtests=$numtests+1
tempfile=$(mktmp)
TOKTX_OPTIONS=--lower_left_maps_to_s0t0 $toktx --nometadata - ../srcimages/rgb.ppm > $tempfile
cmpktx $tempfile rgb-reference.ktx

echo "Tests run: $numtests; passed: $passed; failed: $failed"
if [ $failed -gt 0 ]; then
  exit 1;
else
  exit 0;
fi

